﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using NazSoft.Framework.UsersManagement;
using NazSoft.Framework;
using System.Linq;



public partial class UserControls_TopNavigationMenu : System.Web.UI.UserControl
{
    List<SitePagesInfo> menuList = new List<SitePagesInfo>();
    //List<Category> categoryList = new List<Category>();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            CreateNavigationMenu();
        }
    }

    /// <summary>
    /// create asp menu and add items
    /// </summary>
    private void CreateNavigationMenu()
    {

        menuList = SessionHandler.GetItem(SessionHandler.SessionItems.UserPrivileges) as List<SitePagesInfo>;

        if (menuList == null)
        {
            return;
        }


        LogoutLinkPlaceholder.Visible = true;

        List<SitePagesInfo> menuList1 = menuList.Where(f => f.PageParentID == 0 && f.IsVisibleInMenu).OrderBy(x => x.OrderId).ToList();

        StringBuilder menuBuilder = new StringBuilder();
        menuBuilder.AppendLine("<ul class=\"topnav\">");

        foreach (SitePagesInfo menuItem in menuList1)
        {
            menuBuilder.AppendLine("<li>");

            //<a href="#">Home</a>
            menuBuilder.AppendLine("<a href=\"" + menuItem.PageURL.Replace("~",string.Empty) + "\">" + menuItem.PageName + "</a>");

            //if (menuItem.PageParentID!=0)
            CreateChildItems(menuItem.PageId, menuBuilder);

            menuBuilder.AppendLine("</li>");
        }

        menuBuilder.AppendLine("</ul>");
        MenuHTML.Text = menuBuilder.ToString();
    }


    /// <summary>
    /// Add child items to the parent node.
    /// </summary>
    /// <param name="item"></param>
    /// <param name="menuId"></param>
    private void CreateChildItems(int menuId, StringBuilder menuBuilder)
    {
        menuBuilder.AppendLine("<ul class=\"subnav\">");

        menuList = SessionHandler.GetItem(SessionHandler.SessionItems.UserPrivileges) as List<SitePagesInfo>;

        List<SitePagesInfo> menuList1 = menuList.Where(f => f.PageParentID == menuId && f.PageParentID != 0 && f.IsVisibleInMenu).OrderBy(x => x.OrderId).ToList();
        foreach (SitePagesInfo oCategory in menuList1)
        {
            menuBuilder.AppendLine("<li>");

            menuBuilder.AppendLine("<a href=\"" + oCategory.PageURL.Replace("~", string.Empty) + "\">" + oCategory.PageName + "</a>");

            menuBuilder.AppendLine("</li>");
        }

        menuBuilder.AppendLine("</ul>");
    }
}
